$(function() {
	var id = getvl("sortTypeId");
	if(id != '') {
		searchBySortType(id);
	}

	var goodsType = getvl("goodsType");

	var isReturnedOfSevenDays = getvl("isReturnedOfSevenDays");
	var tradeNumber = getvl("tradeNumber");
	if(goodsType != '' || isReturnedOfSevenDays != '' || tradeNumber != '') {
		selectGoods(tradeNumber, goodsType, isReturnedOfSevenDays);
	}

   //搜索框
   $('.goods').addClass('curr').siblings().removeClass('curr');
   $('#goods').css("display", "block");
   $('#shop').css("display", "none");
});

var isIndex = getvl("isIndex");
var goldRate = getvl("goldRate");
var goodsTitle = getvl1("goodsTitle");
if(isIndex != '' || goodsTitle != '') {
	loadMyEssay('', '', '', '', '', '', '', '', isIndex, goodsTitle);
}
//商圈
TradeArea();
//分类
sortType();
//属性
propertyType();

var currentPage = 1;
var tradeAreaId = '';
var sortTypeId = '';
var propertyTypeId = '';
var tradeNumber = '',
	goodsType = '',
	isReturnedOfSevenDays = '';
var sort = ''; //排序
var isIndex = ''; //推荐到首页

//价格区间
var betweenMoney = '';

//列表
loadMyEssay(tradeAreaId, sortTypeId, propertyTypeId, tradeNumber, goodsType, isReturnedOfSevenDays, sort, betweenMoney, isIndex, goodsTitle);

function loadMyEssay(tradeAreaId, sortTypeId, propertyTypeId, tradeNumber, goodsType, isReturnedOfSevenDays, sort, betweenMoney, isIndex, goodsTitle) {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#goodsList").html("");
		$.ajax({
			url: urlcore + "/api/goods/selectGoodsList?tradeAreaId=" + tradeAreaId + "&sortTypeId=" + sortTypeId + "&propertyTypeId=" + propertyTypeId + "&tradeNumber=" + tradeNumber + "&goodsType=" + goodsType + "&isReturnedOfSevenDays=" + isReturnedOfSevenDays + "&sort=" + sort + "&betweenMoney=" + betweenMoney + "&isIndex=" + isIndex + "&goodsTitle=" + goodsTitle + "&size=" + 32 + "&current=" + pageNo,
			type: "get",
			dataType: 'json',
			async: 'false',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				//$('#goodsName').val(goodsTitle);
				if(data.success == true) {
					$.each(data.data.list, function(i, n) {
						
						var coverUrl=n.coverUrl==null?'http://app.jiagangwangluo.com/api//ATTACHMENT/cab06117-6607-401a-843d-5ac103323fb6/default.png':n.coverUrl;
						coverUrl=isHasImg(coverUrl);//图片处理
						var goodsTitle=n.goodsTitle==null?'暂无':n.goodsTitle;
						var tradePrice=n.tradePrice==null?'0':n.tradePrice;
						var packPrice=n.packPrice==null?'0':n.packPrice;
						var collectNum=n.collectNum==null?'0':n.collectNum;
						
						var thislist =
							'<dl>' +
							'<a target="_blank" href="2.2.0goods_detail.html?id=' + n.id + '&breadcrumbType=1">' +
							'<dt>' +
							'<img class="searchZhaoHuoImg" src="' + coverUrl + '" />' +
							'</dt>' +
							'<dd>' +
							'<h5>' + goodsTitle + '</h5>' +
							'<h6>&yen;' + tradePrice + '-' + packPrice + '<span>收藏 ' + collectNum + '</span>' + '</h6>' +
							'</dd>' +
							'</a>' +
							'</dl>';
						$('#goodsList').append(thislist);
					});
					
					//固定宽高
					$(function(){
						var imgList1 = document.getElementsByClassName("searchZhaoHuoImg");  
					    var w=290;
					    var h=290;
					    for(var i=0;i<imgList1.length;i++){
					        if(imgList1[i].width>w || imgList1[i].height>h){  
					            imgList1[i].width = w;  
					            imgList1[i].heigth = h;  
					        }  
					    }  
					})

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					//alert(thisUrl);
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						layer.alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				layer.alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

/*function TradeArea() {
	$("#tradeArea").html("");
	$.ajax({
		url: urlcore + "/api/tradeArea/selectList",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$.each(data.data, function(i, n) {
					var thislist = '<a href="javascript:searchByTradeArea(' + n.id + ');">' + '<span class="tags">' + n.areaName + '</span>' + '</a>';
					$('#tradeArea').append(thislist);
				});
			}
		},
		error: function() {
			alert("error");
		}
	});
}*/
//按区域查询
function searchByTradeArea(tradeAreaId) {
	loadMyEssay(tradeAreaId, '', '', '', '', '', '', '', '', '');
}

/*function sortType() {
	$("#sortType").html("");
	$.ajax({
		url: urlcore + "/api/sortType/allSortTypeList",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {

			if(data.success == true) {
				$.each(data.data, function(i, n) {
					var thislist = '<a href="javascript:searchBySortType(' + n.id + ');">' + '<span class="tags">' + n.sortName + '</span>' + '</a>';
					$('#sortType').append(thislist);
				});
			}
		},
		error: function() {
			alert("error");
		}
	});
}*/
//按分类查询
function searchBySortType(sortTypeId) {
	$.ajax({
		url: urlcore + "/api/sortType/selectOne?id=" + sortTypeId,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				$("#breadcrumbName").html("");
				var breadcrumbName = '<a href="javascript:searchBySortType(' + data.data.id + ');"><span style="color: #FE7948;">' + data.data.sortName + '</span></a>';
				$("#breadcrumbName").html(breadcrumbName);
			}
		},
		error: function() {
			layer.alert("error");
		}
	});

	loadMyEssay('', sortTypeId, '', '', '', '', '', '', '', '');
}

//按属性查询
function searchByPropertyType(propertyTypeId) {
	loadMyEssay('', '', propertyTypeId, '', '', '', '', '', '', '');
}

//搜索 一件起批   热销商品   支持包换
function selectGoods(tradeNumber, goodsType, isReturnedOfSevenDays) {

	loadMyEssay('', '', '', tradeNumber, goodsType, isReturnedOfSevenDays, '', '', '', '');
}
//按时间,单价,收藏量,销售量排序
function selectGoodsOrderBySort(sort) {
	loadMyEssay('', '', '', '', '', '', sort, '', '', '');
}
//按价格区间以及起批量查询 
function selectGoodsByMoneyOrTradeNumber() {
	var minPprice = $("#min_price").val().trim();
	var maxPrice = $("#max_price").val().trim();
	var betweenMoney = '';
	if(minPprice != '' && minPprice != null && maxPrice != '' && maxPrice != null) {
		betweenMoney = minPprice + "-" + maxPrice;
	}
	var retailPum = $("#retail_num").val().trim();

	loadMyEssay('', '', '', retailPum, '', '', '', betweenMoney, '', '');
}

 
